const express = require('express')
const path = require('path')
const fs = require('fs')
const app = new express()
const port = process.env.PORT || 5500 // 设定监听端口
// 日志依赖包
const FileStreamRotator = require('file-stream-rotator')
const logger = require('morgan')

// 跨域
const cors = require('cors')
app.use(cors())

// 引入上传文件相关模块
const fileUpload = require('express-fileupload')
app.use(
  fileUpload({
    useTempFiles: true, // 使用临时文件
    tempFileDir: '/tmp/', // 临时路径
    createParentPath: true, // 自动创建 .mv(filePathName) 中指定的目录路径
  })
)

app.use(express.json())
app.use(express.urlencoded())
app.use('/static', express.static(path.join(__dirname, 'static')))

// 注册和导入路由模块
app.use('/', require('./src/routes/index'))
app.use('/api/v1', require('./src/routes/v1/upload'))
// 用户相关接口
app.use('/api/v1/user', require('./src/routes/v1/user/register'))
app.use('/api/v1/user', require('./src/routes/v1/user/login'))
app.use('/api/v1/user', require('./src/routes/v1/user/auth'))
app.use('/api/v1/user', require('./src/routes/v1/user/getUserInfoList'))
app.use('/api/v1/user', require('./src/routes/v1/user/update/info'))
app.use('/api/v1/user', require('./src/routes/v1/user/avatar/upload'))
app.use('/api/v1/user', require('./src/routes/v1/user/update/password'))
app.use('/api/v1/user', require('./src/routes/v1/user/user-role/index'))
app.use('/api/v1/user', require('./src/routes/v1/user/user-role/getUserRole'))
// 发送验证码
app.use('/api/v1/user', require('./src/routes/v1/user/send-mail'))
app.use('/api/v1/user', require('./src/routes/v1/user/mail-login'))
app.use(
  '/api/v1/user',
  require('./src/routes/v1/user/user-role/deleteUserRole')
)
// 获取用户评论点赞列表
app.use('/api/v1/user', require('./src/routes/v1/user/get-user-comment-like'))
// 获取用户资讯点赞列表
app.use('/api/v1/user', require('./src/routes/v1/user/get-user-news-like'))
// 获取用户资讯点击列表
app.use('/api/v1/user', require('./src/routes/v1/user/get-user-news-hits'))

// 菜单相关接口
app.use('/api/v1/menu', require('./src/routes/v1/menu/getMenuListTotal'))
app.use('/api/v1/menu', require('./src/routes/v1/menu/add/addMenu'))
app.use('/api/v1/menu', require('./src/routes/v1/menu/update/index'))
app.use('/api/v1/menu', require('./src/routes/v1/menu/get-child-menu'))
// 角色相关接口
app.use('/api/v1/role', require('./src/routes/v1/role/getRoleListTotal'))
app.use('/api/v1/role', require('./src/routes/v1/role/add/addRole'))
app.use('/api/v1/role', require('./src/routes/v1/role/update/index'))
app.use('/api/v1/role', require('./src/routes/v1/role/role-menu/index'))
app.use('/api/v1/role', require('./src/routes/v1/role/role-menu/getRoleMenu'))
app.use(
  '/api/v1/role',
  require('./src/routes/v1/role/role-menu/deleteRoleMenu')
)
// 栏目相关接口
app.use('/api/v1/class', require('./src/routes/v1/class/add-class'))
app.use('/api/v1/class', require('./src/routes/v1/class/update-class'))
app.use('/api/v1/class', require('./src/routes/v1/class/get-class'))
app.use('/api/v1/class', require('./src/routes/v1/class/get-child-class'))

// 资讯相关接口
app.use('/api/v1/news', require('./src/routes/v1/news/add-news'))
app.use('/api/v1/news', require('./src/routes/v1/news/get-news'))
app.use('/api/v1/news', require('./src/routes/v1/news/update-news'))
app.use('/api/v1/news', require('./src/routes/v1/news/get-news-content'))
app.use('/api/v1/news', require('./src/routes/v1/news/news-like'))
app.use('/api/v1/news', require('./src/routes/v1/news/get-news-like-list'))

// 评论相关接口
app.use('/api/v1/comment', require('./src/routes/v1/comment/get-comment'))
app.use('/api/v1/comment', require('./src/routes/v1/comment/add-comment'))
app.use(
  '/api/v1/comment',
  require('./src/routes/v1/comment/get-comment-like-list')
)
app.use('/api/v1/comment', require('./src/routes/v1/comment/comment-like'))

app.use(function (req, res, next) {
  return res.status(404).send("Sorry can't find that!")
})

const logDirectory = path.join(__dirname, 'log')
// 确保存储的路径存在
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
// 创建输出流
var errorLogStream = FileStreamRotator.getStream({
  date_format: 'YYYYMMDD', //日期类型
  filename: path.join(logDirectory, '%DATE%-error.log'), //文件名
  frequency: 'daily', //每天的频率
  verbose: false,
})
// 创建输出流
var accessLogStream = FileStreamRotator.getStream({
  date_format: 'YYYYMMDD',
  filename: path.join(logDirectory, '%DATE%-access.log'),
  frequency: 'daily',
  verbose: false,
})
//写正常访问请求的log日志
app.use(
  logger(
    ':localDate :remote-addr :method :url :status :res[content-length] - :response-time ms',
    { stream: accessLogStream }
  )
)
//写访问出错的log日志
app.use(
  logger(
    ':localDate :remote-addr :method :url :status :res[content-length] - :response-time ms',
    {
      skip: function (req, res) {
        return res.statusCode < 400
      },
      stream: errorLogStream,
    }
  )
)

app.listen(port, function () {
  console.log(`服务启动成功 http://localhost:${port}`)
})
